package example;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 配置管理器单例
 */
public class ConfigurationManager {
    
    private static class SingletonHolder {
        private static final ConfigurationManager INSTANCE = new ConfigurationManager();
    }
    
    private final Map<String, String> configMap;
    
    private ConfigurationManager() {
        configMap = new HashMap<>();
        loadConfiguration();
    }
    
    public static ConfigurationManager getInstance() {
        return SingletonHolder.INSTANCE;
    }
    
    private void loadConfiguration() {
        try {
            Properties properties = new Properties();
            properties.load(getClass().getClassLoader().getResourceAsStream("application.properties"));
            
            for (String key : properties.stringPropertyNames()) {
                configMap.put(key, properties.getProperty(key));
            }
            
            System.out.println("配置加载完成，共加载 " + configMap.size() + " 个配置项");
        } catch (Exception e) {
            throw new RuntimeException("加载配置文件失败", e);
        }
    }
    
    public String getProperty(String key) {
        return configMap.get(key);
    }
    
    public String getProperty(String key, String defaultValue) {
        return configMap.getOrDefault(key, defaultValue);
    }
    
    public int getIntProperty(String key, int defaultValue) {
        String value = configMap.get(key);
        if (value != null) {
            try {
                return Integer.parseInt(value);
            } catch (NumberFormatException e) {
                // 记录日志
            }
        }
        return defaultValue;
    }
    
    public boolean getBooleanProperty(String key, boolean defaultValue) {
        String value = configMap.get(key);
        if (value != null) {
            return Boolean.parseBoolean(value);
        }
        return defaultValue;
    }
}